﻿using Administratie_lib;
using DataAccess;
using Reparatie;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SedatGroep6
{
    public partial class Form1 : Form
    {
        private Database db;
        bool nietOpVoorraad = false;
        bool reparatie = false;
        private Bestelling bestelling;
        public Form1()
        {
            InitializeComponent();
            db = new Database();
            addColumns();
            bestelling = new Bestelling();
        }

        private void Assemblage_Load(object sender, EventArgs e)
        {
            UpdateOrders();
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            string update = "";
            if (rbBehandeling.Checked)
                update = "In behandeling";
            else if (rbWachten.Checked)
                update = "Wacht op voorraad";
            else if (rbLeverancier.Checked)
                update = "Aan leverancier aangeboden";
            else if (rbPakketdienst.Checked)
                update = "Aan pakketdienst aangeboden";
            else if (rbRepareren.Checked)
                update = "Reparatie";

            Assemblage_lib.I_Assemblage assemblage = new Assemblage_lib.Assemblage();
            ReparatieClass reparatie = new ReparatieClass();
            assemblage.Assembleer(update, GetOrderID());

            if (update == "Reparatie")
            {
                ReparatieClass rep = new ReparatieClass();
                rep.Repareer(GetOrderID());
            }

            UpdateOrders();


        }

        private int GetOrderID()
        {
            return Convert.ToInt32(lbOrders.FocusedItem.Text);
        }

        private void UpdateOrders()
        {
            gbUpdateStatus.Visible = false;
            lbProducten.Items.Clear();
            lbOrders.Items.Clear();
            addColumns();
            DataTable resultOrder = bestelling.Zoek();

            foreach (DataRow row in resultOrder.Rows)
            {
                double totalPrice = 0;
                string status = "", soortOrder = "";
                DataTable prices = bestelling.Zoek(Convert.ToInt32(row["Id"])); 
                foreach (DataRow priceRows in prices.Rows)
                {

                    totalPrice += (Convert.ToDouble(priceRows["aantal"]) * Convert.ToInt32(priceRows["prijs"]));
                    status = priceRows["StatusOmschrijving"].ToString();
                    soortOrder = priceRows["SoortOrder"].ToString();

                }
                string[] data = new string[] { row["id"].ToString(), "€" + totalPrice, status, soortOrder };
                ListViewItem dataListView = new ListViewItem(data);
                lbOrders.Items.Add(dataListView);
            }
            rbPakketdienst.Enabled = false;
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            UpdateOrders();
        }

        private void addColumns()
        {
            lbOrders.Columns.Clear();
            lbOrders.Columns.Add("ID");
            lbOrders.Columns.Add("Prijs");
            lbOrders.Columns.Add("Status",180);
            lbOrders.Columns.Add("Soort Order", 180);
        }

        private void lbOrders_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            lbProducten.Items.Clear();
            rbBehandeling.Checked = true;
            reparatie = false;
            nietOpVoorraad = false;

            if (lbOrders.FocusedItem != null)
            {
                addProductColumns();
                DataTable producten = bestelling.Zoek(GetOrderID());
                foreach (DataRow productdetails in producten.Rows)
                {
                    string[] data = new string[] { productdetails["Id"].ToString(), productdetails["Naam"].ToString(), productdetails["Aantal"].ToString(), productdetails["Voorraad"].ToString() };
                    ListViewItem dataListView = new ListViewItem(data);
                    if(Convert.ToInt32(productdetails["Aantal"]) > Convert.ToInt32(productdetails["Voorraad"]))
                    {
                        dataListView.BackColor = Color.Red;
                        nietOpVoorraad = true;
                    }
                    if (productdetails["SoortOrder"].ToString() == "Reparatie")
                    {
                        reparatie = true;
                    }
                    lbProducten.Items.Add(dataListView);
                }
                rbPakketdienst.Enabled = false;
                gbUpdateStatus.Visible = true;
                if (!reparatie)
                {
                    rbLeverancier.Enabled = false;
                    rbRepareren.Enabled = false;
                }
                else
                {
                    rbLeverancier.Enabled = true;
                    rbRepareren.Enabled = true;
                }
            }
        }

        private void addProductColumns()
        {
            lbProducten.Columns.Clear();
            lbProducten.Columns.Add("ID");
            lbProducten.Columns.Add("Naam", 180);
            lbProducten.Columns.Add("Aantal");
            lbProducten.Columns.Add("In voorraad");
        }

        private void lbProducten_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            bool notCheck = false;
            int counter = 0;
            while (!notCheck && counter < lbProducten.Items.Count)
            {
                if (!lbProducten.Items[counter].Checked)
                {
                    notCheck = true;
                }
                counter++;
            }
            if (!notCheck && !nietOpVoorraad)
            {
                rbPakketdienst.Enabled = true;
            }
            else
            {
                rbPakketdienst.Enabled = false;
            }
        }
    }
}
